# -*- coding: utf-8 -*-
import KBEngine
import time
from KBEDebug import *
from server_common import SCDefine
from interfaces.CharacterPropertys import CharacterPropertys
from interfaces.CharacterAttCombination import CharacterAttCombination
from interfaces.CharacterProfession import CharacterProfession
from interfaces.CharacterLevel import CharacterLevel
from interfaces.CharacterZhanYi import CharacterZhanYi
from gameComponent.BaseInfoComponent import BaseInfoComponent

import s_systemConfig

class Avatar(KBEngine.Proxy,
             CharacterAttCombination,
             CharacterPropertys,
             CharacterProfession,
             CharacterLevel,
             CharacterZhanYi):
    def __init__(self):
        KBEngine.Proxy.__init__(self)
        CharacterAttCombination.__init__(self)
        CharacterPropertys.__init__(self)
        CharacterProfession.__init__(self)
        CharacterLevel.__init__(self)
        CharacterZhanYi.__init__(self)
        self._destroyTimer = 0
        self.baseInfo = BaseInfoComponent(self, self.id, self.name)
        self.accountEntity = None
        self.updateCharacterAttribute()
        self.setGuanQia(self._guanqia)

    def getBaseID(self):
        return self.baseInfo.id

    def destroySelf(self):
        """
        """
        if self.client is not None:
            return

        if self.cell is not None:
            # 销毁cell实体
            self.destroyCellEntity()
            return

        # 如果帐号ENTITY存在 则也通知销毁它
        if self.accountEntity is not None:
            if time.time() - self.accountEntity.relogin > 1:
                self.accountEntity.destroy()
            else:
                DEBUG_MSG("Avatar[%i].destroySelf: relogin =%i" % (self.id, time.time() - self.accountEntity.relogin))

        # 销毁base
        if not self.isDestroyed:
            self.destroy()

    # --------------------------------------------------------------------------------------------
    #                              Callbacks
    # --------------------------------------------------------------------------------------------
    def onTimer(self, tid, userArg):
        """
        KBEngine method.
        引擎回调timer触发
        """
        # DEBUG_MSG("%s::onTimer: %i, tid:%i, arg:%i" % (self.getScriptName(), self.id, tid, userArg))
        if SCDefine.TIMER_TYPE_DESTROY == userArg:
            self.onDestroyTimer()

        # GameObject.onTimer(self, tid, userArg)

    def onGetCell(self):
        """
        KBEngine method.
        entity的cell部分实体被创建成功
        """
        DEBUG_MSG('Avatar::onGetCell: %s' % self.cell)

    def createCell(self, space):
        """
        defined method.
        创建cell实体
        """
        self.createCellEntity(space)

    def onClientDeath(self):
        """
        KBEngine method.
        entity丢失了客户端实体
        """
        DEBUG_MSG("Avatar[%i].onClientDeath:" % self.id)
        # 防止正在请求创建cell的同时客户端断开了， 我们延时一段时间来执行销毁cell直到销毁base
        # 这段时间内客户端短连接登录则会激活entity
        self._destroyTimer = self.addTimer(10, 0, SCDefine.TIMER_TYPE_DESTROY)

    def onClientGetCell(self):
        """
        KBEngine method.
        客户端已经获得了cell部分实体的相关数据
        """
        INFO_MSG("Avatar[%i].onClientGetCell:%s" % (self.id, self.client))

    def onDestroyTimer(self):
        DEBUG_MSG("Avatar::onDestroyTimer: %i" % self.id)
        self.destroySelf()

    def onDestroy(self):
        """
        KBEngine method.
        entity销毁
        """
        DEBUG_MSG("Avatar::onDestroy: %i." % self.id)
        if self.accountEntity is not None:
            self.accountEntity.activeAvatar = None
            self.accountEntity = None

    def reqFight(self, reqData):
        re = self.doZhangJie(1, 0)
        if self.client and re:
            self.client.onFightSync(re)

    def getSystemTotalTimes(self, systemId):
        """获取系统可使用总次数"""
        systemInfo = s_systemConfig.datas.get(systemId, None)
        if not systemInfo:
            return 0
        sysTotalTimes = systemInfo.get('canUseTimes', 0) + vipManager.GetVipRewardSystemTimes(self.level.getVipLevel(),systemId)
        # if systemId == dbGameConfig.TEAM_FIGHT_MAX:
        #     print u'账号名:%s,组队战次数:%d,vip等级:%d'%(self.baseInfo.getNickName(),sysTotalTimes,self.level.getVipLevel())
        return sysTotalTimes


